package com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.TongJiInfo;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.dao.CaseSelectionAdminDao;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.ICaseSelectionServiceAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CaseSelectionServiceAdminImpl implements ICaseSelectionServiceAdmin {
    @Autowired
    private CaseSelectionAdminDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    public List<CaseSelectionAdminInfo> selectPageListByQueryGetMap(Page<CaseSelectionAdminInfo> page, SearchCaseSelectionAdminInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getIsSelection())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("isSelection", " and t.distribution_status = " + searchInfo.getIsSelection() + " ");
                params.put("orderBy", " order by t.apply_time desc ");
            } else {
                params.put("isSelection", " and t.distribution_status = " + searchInfo.getIsSelection() + " ");
                params.put("orderBy", " order by t.selection_time desc ");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("startTime", " and t.apply_time >= '" + searchInfo.getStartTime() + "'");
            } else {
                params.put("startTime", " and t.distribution_time >= '" + searchInfo.getStartTime() + "'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("endTime", " and t.apply_time <= '" + searchInfo.getEndTime() + "'");
            } else {
                params.put("endTime", " and t.distribution_time <= '" + searchInfo.getEndTime() + "'");
            }
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPages(page, params);
    }

    public int getQueryPagesCount(Page<CaseSelectionAdminInfo> page, SearchCaseSelectionAdminInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsSelection())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("isSelection", " and t.distribution_status = " + searchInfo.getIsSelection() + " ");
                params.put("orderBy", " order by t.apply_time desc ");
            } else {
                params.put("isSelection", " and t.distribution_status = " + searchInfo.getIsSelection() + " ");
                params.put("orderBy", " order by t.distribution_time desc ");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("startTime", " and t.apply_time >= '" + searchInfo.getStartTime() + "'");
            } else {
                params.put("startTime", " and t.distribution_time >= '" + searchInfo.getStartTime() + "'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("endTime", " and t.apply_time <= '" + searchInfo.getEndTime() + "'");
            } else {
                params.put("endTime", " and t.distribution_time <= '" + searchInfo.getEndTime() + "'");
            }
        }
        return dao.getQueryPagesCount(page, params);
    }

    public List<CaseSelectionAdminInfo> selectPageListByQueryGetGoBack(Page<CaseSelectionAdminInfo> page, SearchCaseSelectionAdminInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", " and t1.back_time >= '" + searchInfo.getStartTime() + "'");
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", " and t1.back_time <= '" + searchInfo.getEndTime() + "'");
        }
        params.put("backAdmin", " and t1.back_admin='" + ShiroKit.getUser().getAccount() + "'");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.selectPageListByQueryGetGoBack(page, params);
    }

    public int getByQueryGetGoBackPagesCount(Page<CaseSelectionAdminInfo> page, SearchCaseSelectionAdminInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", " and t1.back_time >= '" + searchInfo.getStartTime() + "'");
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", " and t1.back_time <= '" + searchInfo.getEndTime() + "'");
        }
        params.put("backAdmin", " and t1.back_admin='" + ShiroKit.getUser().getAccount() + "'");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getByQueryGetGoBackPagesCount(page, params);
    }

    public List<TongJiInfo> caseSelectiontotal(SearchCaseSelectionAdminInfo searchInfo) {
        String contitionInfo = " ";
        String contitionInfo1 = " ";
        String contitionInfo2 = " ";
        if (StringUtil.isNotEmpty(searchInfo.getIsSelection())) {
            if (searchInfo.getIsSelection() == 0) {
                contitionInfo += " and t.distribution_status=0";
                contitionInfo1 += " and t.distribution_status=1";
            } else {
                contitionInfo += " and t.distribution_status=0";
                contitionInfo1 += " and t.distribution_status=1";
            }
        }
        contitionInfo2 += " and t1.back_admin='" + ShiroKit.getUser().getAccount() + "' ";
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            if (searchInfo.getIsSelection() == 0) {
                contitionInfo += " and t.apply_time >= '" + searchInfo.getStartTime() + "' ";
            } else if (searchInfo.getIsSelection() == 1) {
                contitionInfo1 += " and t.distribution_time >= '" + searchInfo.getStartTime() + "' ";
            } else {
                contitionInfo2 += " and t1.back_time <= '" + searchInfo.getEndTime() + "' ";
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            if (searchInfo.getIsSelection() == 0) {
                contitionInfo += " and t.apply_time <= '" + searchInfo.getEndTime() + "' ";
            } else if (searchInfo.getIsSelection() == 1) {
                contitionInfo1 += " and t.distribution_time <= '" + searchInfo.getEndTime() + "' ";
            } else {
                contitionInfo2 += " and t1.back_time <= '" + searchInfo.getEndTime() + "' ";
            }
        }
        String sql = "select count(*) as countNum from (select t.id from case_selection_information t left join case_selection_info t1 on t.id=t1.information_id " +
                " where t.save_status =1 " + contitionInfo + " group by t.id) t " +  //查询待提交病例
                " union all " +
                " select count(*) as countNum from (select t.id from case_selection_information t left join case_selection_info t1 on t.id=t1.information_id " +
                " where t.save_status =1 " + contitionInfo1 + " group by t.id) t" +  //查询已提交病例
                " union all " +
                "  select count(1) as countNum from (select t.id as id  from  go_back_case_info t1 left join case_selection_information t on t.id=t1.backed_id " +
                "  where 1=1 " + contitionInfo2 + " ) t";  //回退记录
        return sqlMapper.selectList(sql, TongJiInfo.class);
    }
}